package com.graduate.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.graduate.common.entity.House;
import com.graduate.common.entity.Reserve;
import com.graduate.common.entity.Result;
import com.graduate.common.vo.HouseVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bwzfy
 * @create 2021/5/14
 **/
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    @ResponseBody
    @GetMapping("/list")
    public Result<IPage<HouseVo>> houseList(@RequestParam(required = false) Map<String, Object> map) {
        Result<IPage<HouseVo>> result = new Result<>();
        result.setCode(200);
        result.setData(houseService.selectHousePage(map));
        return result;
    }

    @ResponseBody
    @GetMapping("/search/list/{date}")
    public Result<IPage<HouseVo>> searchEmptyHouse(@RequestParam(required = false) Map<String, Object> map, @PathVariable("date") String date) throws ParseException {
        // 先查询出办理表中的endTime
        IPage<HouseVo> houseList = houseService.selectHousePage(map);
        QueryWrapper<Reserve> wrapper = new QueryWrapper<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        wrapper.gt("endTime", sdf.parse(date));
        List<Reserve> reserves = reserveService.list(wrapper);
        List<HouseVo> collect = houseList.getRecords().stream().filter(houseVo -> {
            for (int i = 0; i < reserves.size(); i++) {
                Reserve reserve = reserves.get(i);
                if (reserve.getHid().equals(houseVo.getHid())) {
                    return false;
                }
            }
            return true;
        }).collect(Collectors.toList());
        houseList.setRecords(collect);
        Result<IPage<HouseVo>> result = new Result<>();
        result.setCode(200);
        result.setData(houseList);
        return result;
    }

}
